/**
 * PathSegmentFactory.java
 * 2016年6月4日
 * ©2015 北京特微智能科技有限公司. All rights reserved.
 */
package cn.trawe.pathmappingtree;

import java.util.Optional;

/**
 * 根据真实的字符串，生成PathSegment的工厂类
 * @author wangda
 */
public class PathSegmentFactory {
    
    private static String START_CHAR = "{";
    
    private static String END_CHAR = "}";
    
    public static Optional<PathSegment> newPathSegment(String seg) {
        seg = seg == null? "": seg.trim();
        if (seg.startsWith(START_CHAR) && seg.endsWith(END_CHAR)) { // 正则表达式
            String regexStr = seg.substring(1, seg.length()-1);
            return Optional.of((PathSegment)new RegexPathSegment(regexStr));
        } else if (seg.indexOf("*") > -1) { // 通配符
            return Optional.of((PathSegment)new WildcardPathSegment(seg));
        } else if (seg.length() > 0) {
            return Optional.of((PathSegment)new NormalPathSegment(seg));
        } else {
            return Optional.ofNullable(null);
        }
    }
}
